package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Slf4j
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo> implements ApartmentInfoService {

    //    图片表
    @Autowired
    private GraphInfoService graphInfoService;

    //    公寓 和 标签之间的中间表
    @Autowired
    private ApartmentLabelService apartmentLabelService;

    //    公寓 和 配套 之间的 中间表
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    //    公寓 和 杂费值之间的中间表
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;


    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private  ProvinceInfoMapper  provinceInfoMapper;

    @Autowired
    private  CityInfoMapper  cityInfoMapper;

    @Autowired
    private   DistrictInfoMapper districtInfoMapper;

    //    根据 公寓的 id 删除 公寓的各个表之间的相关信息
    @Override
    public void removeApartmentById(Long id) {

//        删除公寓，如果发现 当前的公寓下面还有房间， 那么就不能删除，抛出异常
        LambdaQueryWrapper<RoomInfo> lambdaRoomInfoWrapper = new LambdaQueryWrapper<>();
        lambdaRoomInfoWrapper.eq(RoomInfo::getApartmentId, id);

        Long roomSize = roomInfoMapper.selectCount(lambdaRoomInfoWrapper);

        if (roomSize > 0) {
            throw new LeaseException(ResultCodeEnum.ADMIN_APARTMENT_DELETE_ROOM);
        }
//       1、删除 ApartmentInfo 中的 公寓信息
        super.removeById(id);

//       2、删除图片列表
        LambdaQueryWrapper<GraphInfo> lambdaGraphInfoWrapper = new LambdaQueryWrapper<>();
        lambdaGraphInfoWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        lambdaGraphInfoWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(lambdaGraphInfoWrapper);

//       3、删除配套列表
        LambdaQueryWrapper<ApartmentFacility> lambdaFacilityWrapper = new LambdaQueryWrapper<>();
        lambdaFacilityWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(lambdaFacilityWrapper);

//       4、删除 标签 列表
        LambdaQueryWrapper<ApartmentLabel> lambdaLabelWrapper = new LambdaQueryWrapper<>();
        lambdaLabelWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(lambdaLabelWrapper);
//       5、删除杂费列表
        LambdaQueryWrapper<ApartmentFeeValue> lambdaFeeValueWrapper = new LambdaQueryWrapper<>();
        lambdaFeeValueWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(lambdaFeeValueWrapper);
    }

    //    根据 公寓id 查询公寓的 详情信息
    @Override
    public ApartmentDetailVo getDetailById(Long id) {

//       1、查询公寓的 信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);

//        2、查询图片列表
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);

//        3、查询标签列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByItemLabelId(id);

//        4、查询配套列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByItemFacilityId(id);

//        5、查询杂费列表
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByItemFeeValueId(id);

//        创建 apartmentDetailVo 对象，进行 封装数据
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();

//        将 apartmentInfo 中 和 apartmentDetailVo 同名的属性 进行赋值
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);

//        封装数据
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        log.info("apartmentDetailVo:{}", apartmentDetailVo);
        return apartmentDetailVo;
    }

    /***
     *   根据条件分页查询公寓列表
     * @param page
     * @param queryVo
     * @return
     */
    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {

        IPage<ApartmentItemVo> iPage = apartmentInfoMapper.pageItem(page, queryVo);

        return iPage;
    }

    /***
     *  保存或更新公寓信息
     * @param apartmentSubmitVo
     */
    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {


//        TODO , 解决前端不显示省、市、县 的 问题
        Long provinceId = apartmentSubmitVo.getProvinceId();
        Long cityId = apartmentSubmitVo.getCityId();
        Long districtId = apartmentSubmitVo.getDistrictId();

        ProvinceInfo provinceInfo = provinceInfoMapper.selectById(provinceId);

        CityInfo cityInfo = cityInfoMapper.selectById(cityId);

        DistrictInfo districtInfo = districtInfoMapper.selectById(districtId);

        apartmentSubmitVo.setProvinceName(provinceInfo.getName());

        apartmentSubmitVo.setCityName(cityInfo.getName());

        apartmentSubmitVo.setDistrictName(districtInfo.getName());


//      1、判断当前是新增还是修改
        boolean isUpdate = apartmentSubmitVo.getId() != null;
//        新增 或者是修改 公寓表中的数据  apartment-info
        super.saveOrUpdate(apartmentSubmitVo);

        if (isUpdate) {
//            说明 当前执行的是修改操作 那么就 删除之前的所有信息
//            1、删除 图片 列表
            LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//                过滤条件 ： 图片所属对象是  公寓 才会被删除
            lambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            lambdaQueryWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoService.remove(lambdaQueryWrapper);

//            2、删除 标签列表
            LambdaQueryWrapper<ApartmentLabel> LabelQueryWrapper = new LambdaQueryWrapper<>();
            LabelQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(LabelQueryWrapper);

//            3、删除 配套列表
            LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            facilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(facilityLambdaQueryWrapper);

//            4、删除杂费值 列表
            LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(feeValueLambdaQueryWrapper);

        }

//        添加 图片列表

//        从前端获取 图片列表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
//        将 GraphVo 对象 转换为  graphInfo 对象
            List<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemId(apartmentSubmitVo.getId());
                //            公寓类型
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());

                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }


//        添加 标签列表
//          从前端 获取 标签列表
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();

        if (!CollectionUtils.isEmpty(labelIds)) {
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = new ApartmentLabel();
                apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                apartmentLabel.setLabelId(labelId);
                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }

//        添加 配套列表

//         从 前端获取 配套列表
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            List<ApartmentFacility> apartmentFacilities = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = new ApartmentFacility();
                apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                apartmentFacility.setFacilityId(facilityInfoId);
                apartmentFacilities.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilities);
        }


//        添加 杂费值 列表
//          从 前端 获取 杂费值列表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            List<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
                apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
                apartmentFeeValue.setFeeValueId(feeValueId);
                apartmentFeeValues.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValues);
        }

    }
}




